﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using OutlookHub.Utils;
using System.Drawing;
using Silvermoon.Images;
using Silvermoon.Windows;
using Silvermoon.OpenGL;
using Silvermoon.Shapes;
using OutlookHub.Images;
using Silvermoon;
using Silvermoon.UI;
using Silvermoon.Animations;

namespace OutlookHub.Classes
{
    /// <summary>
    /// Item with text and a number in a read circle which contains the number of emails or unread emails.
    /// </summary>
    public class TitleCountItem : ContainerItem
    {
        private int count;
        private Label textLabel;
        private Label numberLabel;
        private ScaleTransform countScale;
        private bool isCountVisible = false;

        private static PropertyAccessor<TitleCountItem, float> CountScaleProperty = PropertyAccessor.Register<TitleCountItem, float>("CountScale",
            (c) => c.countScale.X, (c, v) => { c.countScale.X = c.countScale.Y = v; c.numberLabel.Visible = v > 0f; c.Invalidate(InvalidationFlag.Transformation); });

        public TitleCountItem()
            : base()
        {
            textLabel = new Label { Font = FontPool.MediumRegularFont, AutoSize = true, Margin = new Margin(0, 2, 64, null) };
            countScale = new ScaleTransform(0f, 0f, 1f);
            numberLabel = new Label
            {
                Size = new Size(64, 64),
                Background = new ImageShape { Image = ImagePool.Count, Color = PhoneColors.Light },
                Alignment = Alignment.Center,
                LineAlignment = Alignment.Center,
                Margin = new Margin(null, 0, 0, null),
                TextColor = PhoneColors.White,
                FontScale = 0.8f,
                Transformation = countScale,
                Visible = false
            };
            Panel sp = new Panel { Margin = Margin.Zero, Height = 80 };
            sp.Controls.Add(textLabel, numberLabel);
            Control = sp;
        }

        public string Text
        {
            get { return textLabel.Text; }
            set { textLabel.Text = value; }
        }

        public bool IsCountVisible
        {
            get { return isCountVisible; }
            set
            {
                if (isCountVisible != value)
                {
                    isCountVisible = value;
                    OnCountVisibleChanged();
                }
            }
        }


        public int Count
        {
            get { return count; }
            set
            {
                count = value;
                if (value <= 99)
                {
                    numberLabel.Text = value.ToString();
                }
                else
                {
                    numberLabel.Text = "++";
                }

                IsCountVisible = count > 0;
            }
        }

        private void OnCountVisibleChanged()
        {

            if (isCountVisible)
            {
                if (IsInitialized)
                {
                    FloatTransition t = new FloatTransition { Duration = 300, EasingFunction = BackEase.Out, From = 0f, To = 1f };
                    CountScaleProperty.Animate(this, t);
                }
                else
                {
                    CountScaleProperty.SetValue(this, 1f);
                }
            }
            else
            {
                if (IsInitialized)
                {
                    FloatTransition t = new FloatTransition { Duration = 300, EasingFunction = PowerEase.In, To = 0f };
                    CountScaleProperty.Animate(this, t);
                }
                else
                {
                    CountScaleProperty.SetValue(this, 0f);
                }
            }
        }

    }
}
